/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadFuncionario.java
 *
 * Created on 18/10/2011, 19:19:05
 */
package visao;

import cargo.Cargo;
import fachada.FachadaBDR;
import funcionario.Funcionario;
import java.awt.Color;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import login.Login;
import setorEmpresa.SetorEmpresa;
import util.CamposInvalidosException;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;
import util.Util;

/**
 *
 * @author Administrador
 */
public class CadFuncionario extends javax.swing.JFrame {

    /** Creates new form CadFuncionario */
    public CadFuncionario() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/Func.gif")).getImage());
    }

    public CadFuncionario(int idFuncionario) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/Func.gif")).getImage());
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // obtendo os Objetos da base
            Funcionario objFuncionario = objFachadaBDR.procurarFuncionario(idFuncionario);

            // Setando os valores de Funcionario
            jTextField_idFuncionario.setText(String.valueOf(objFuncionario.getIdFuncionario()));
            jTextField_Matricula.setText(objFuncionario.getMatricula());
            jTextField_Cpf.setText(objFuncionario.getCpf());
            jComboBox_Cargo.setSelectedItem(new KeyValue(objFuncionario.getCargo().getDescricaoCargo(), Integer.toString(objFuncionario.getCargo().getIdCargo())));
            jComboBox_SetorEmpresa.setSelectedItem(new KeyValue(objFuncionario.getSetorEmpresa().getDescricaoSetor(), Integer.toString(objFuncionario.getSetorEmpresa().getIdSetorEmpresa())));
            jTextField_Nome.setText(objFuncionario.getNome());
            jCheckBox_Status.setSelected(objFuncionario.getStatusFuncionario());

            // Setando os valores de Login
            jTextField_idLogin.setText(String.valueOf(objFuncionario.getObjLogin().getIdLogin()));
            jPasswordField_Senha.setText(objFuncionario.getObjLogin().getSenha());
            jTextField_Login.setText(objFuncionario.getObjLogin().getLogin());



        } catch (Exception ex) {

            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Mensagem = new javax.swing.JLabel();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel_Login = new javax.swing.JLabel();
        jLabel_Senha = new javax.swing.JLabel();
        jTextField_Login = new javax.swing.JTextField();
        jPasswordField_Senha = new javax.swing.JPasswordField();
        jTextField_idLogin = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_Matricula = new javax.swing.JLabel();
        jLabel_Nome = new javax.swing.JLabel();
        jLabel_Cpf = new javax.swing.JLabel();
        jLabel_Cargo = new javax.swing.JLabel();
        jLabel_Secao = new javax.swing.JLabel();
        jComboBox_SetorEmpresa = new javax.swing.JComboBox();
        jComboBox_Cargo = new javax.swing.JComboBox();
        jTextField_Cpf = new javax.swing.JTextField();
        jTextField_Nome = new javax.swing.JTextField();
        jTextField_Matricula = new javax.swing.JTextField();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jTextField_idFuncionario = new javax.swing.JTextField();

        setTitle("Funcionario");
        setResizable(false);

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel_Login.setText("Login :");

        jLabel_Senha.setText("Senha :");

        jTextField_idLogin.setText("0");
        jTextField_idLogin.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_Senha)
                    .addComponent(jLabel_Login))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 173, Short.MAX_VALUE)
                        .addComponent(jTextField_idLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(76, 76, 76))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPasswordField_Senha, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(283, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField_Login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel_Login)
                                .addGap(26, 26, 26)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel_Senha)
                                    .addComponent(jPasswordField_Senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextField_idLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel_Codigo.setText("Codigo :");

        jLabel_Matricula.setText("Matricula :");

        jLabel_Nome.setText("Nome :");

        jLabel_Cpf.setText("CPF :");

        jLabel_Cargo.setText("Cargo :");

        jLabel_Secao.setText("Seção :");

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            List<SetorEmpresa> listSetorEmpresa = objFachadaBDR.listarSetorEmpresa("");

            jComboBox_SetorEmpresa.addItem(new KeyValue("--- Favor Selecionar ---","0"));
            for (SetorEmpresa objSetorEmpresa : listSetorEmpresa) {
                jComboBox_SetorEmpresa.addItem(new KeyValue(objSetorEmpresa.getDescricaoSetor(), Integer.toString(objSetorEmpresa.getIdSetorEmpresa())));
            }

        } catch (Exception e) {
        }

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            List<Cargo> listCargo = objFachadaBDR.listarCargo("");

            jComboBox_Cargo.addItem(new KeyValue("--- Favor Selecionar ---","0"));
            for (Cargo objCargo : listCargo) {
                jComboBox_Cargo.addItem(new KeyValue(objCargo.getDescricaoCargo(), Integer.toString(objCargo.getIdCargo())));
            }

        } catch (Exception e) {
        }

        jTextField_Cpf = new JFormattedTextField(Util.setMascara("###.###.###-##"));

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jTextField_idFuncionario.setText("0");
        jTextField_idFuncionario.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel_Matricula)
                    .addComponent(jLabel_Codigo)
                    .addComponent(jLabel_Cpf)
                    .addComponent(jLabel_Nome)
                    .addComponent(jLabel_Cargo)
                    .addComponent(jLabel_Secao))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField_Cpf, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_Matricula, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jTextField_idFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 266, Short.MAX_VALUE)
                        .addComponent(jCheckBox_Status)
                        .addGap(52, 52, 52))
                    .addComponent(jTextField_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox_Cargo, 0, 414, Short.MAX_VALUE)
                    .addComponent(jComboBox_SetorEmpresa, 0, 414, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Codigo)
                            .addComponent(jTextField_idFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Matricula)
                            .addComponent(jTextField_Matricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Nome)
                            .addComponent(jTextField_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Cpf)
                            .addComponent(jTextField_Cpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jCheckBox_Status))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel_Cargo))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(jComboBox_Cargo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox_SetorEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Secao))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jButton_Ok))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jButton_Cancelar))
                    .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        } else {
        }


    }//GEN-LAST:event_jButton_CancelarActionPerformed

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:

        int idFuncionario = Integer.parseInt(jTextField_idFuncionario.getText());
        String matricula = jTextField_Matricula.getText();
        String nome = jTextField_Nome.getText();
        String cpf = jTextField_Cpf.getText();
        int idcargo = Integer.parseInt(((KeyValue) jComboBox_Cargo.getSelectedItem()).getValue());
        int idsetor = Integer.parseInt(((KeyValue) jComboBox_SetorEmpresa.getSelectedItem()).getValue());
        Boolean status = jCheckBox_Status.isSelected();

        int idLogin = Integer.parseInt(jTextField_idLogin.getText());

        String lnome = jTextField_Login.getText();
        String senha = jPasswordField_Senha.getText();


        try {

            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // Recuperando o cargo
            Cargo cargo = objFachadaBDR.procurarCargo(idcargo);
            // Recuperando o Setor da empresa
            SetorEmpresa setor = objFachadaBDR.procurarSetorEmpresa(idsetor);
            // Criando o objeto logins
            Login login = new Login(idLogin, lnome, senha);

//            System.out.println("oi"+cpf);
            // Criando o objeto Funcionario
            Funcionario funcionario = new Funcionario(idFuncionario, setor, cargo, login, cpf, nome, matricula, status, null);

            if (idFuncionario == 0) {
                objFachadaBDR.cadastrarFuncionario(funcionario);

                jTextField_idFuncionario.setText("");
                jTextField_Matricula.setText("");
                jTextField_Cpf.setText("");
                jComboBox_Cargo.setSelectedIndex(0);
                jComboBox_SetorEmpresa.setSelectedIndex(0);
                jTextField_Nome.setText("");
                jCheckBox_Status.setSelected(false);

                // Setando os valores de Login

                jPasswordField_Senha.setText("");
                jTextField_Login.setText("");


            } else {
                objFachadaBDR.atualizarFuncionario(funcionario);
            }

            String acao = (idFuncionario == 0) ? "Cadastrado" : "Atualizado";
            jLabel_Mensagem.setText("Funcionario " + acao + " com Sucesso");
            jLabel_Mensagem.setForeground(Color.BLACK);


//            if (idFuncionario == 0) {
//
//                jTextField_Cpf.setText("");
//                jTextField_Login.setText("");
//                jTextField_Matricula.setText("");
//
//                jComboBox_Cargo.setSelectedItem("");
//
//                jPasswordField_Senha.setText("");
//
//
//            }


        } catch (CamposInvalidosException ex) {

            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);


        } catch (Exception ex) {

            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);

        }

    }//GEN-LAST:event_jButton_OkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadFuncionario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JComboBox jComboBox_Cargo;
    private javax.swing.JComboBox jComboBox_SetorEmpresa;
    private javax.swing.JLabel jLabel_Cargo;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Cpf;
    private javax.swing.JLabel jLabel_Login;
    private javax.swing.JLabel jLabel_Matricula;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JLabel jLabel_Nome;
    private javax.swing.JLabel jLabel_Secao;
    private javax.swing.JLabel jLabel_Senha;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField_Senha;
    private javax.swing.JTextField jTextField_Cpf;
    private javax.swing.JTextField jTextField_Login;
    private javax.swing.JTextField jTextField_Matricula;
    private javax.swing.JTextField jTextField_Nome;
    private javax.swing.JTextField jTextField_idFuncionario;
    private javax.swing.JTextField jTextField_idLogin;
    // End of variables declaration//GEN-END:variables
}
